<script setup lang="ts" name="LoginAccount">
import { ref, reactive } from "vue";

import { useLoginStore } from "@/store/login";

const emits = defineEmits(["update:type", "push"]);
// public
const loginLoading = ref(false);
const store = useLoginStore();

// 账号密码登录
const ruleFormRef = ref();
const rules = reactive({
  mobile: [{ required: true, message: "请输入账号", trigger: "blur" }],
  password: [
    {
      required: true,
      message: "请输入密码",
      trigger: "change",
    },
    { min: 0, max: 18, message: "超出字符限制1-18", trigger: "blur" },
  ],
});
const ruleForm = reactive({
  mobile: "",
  password: "",
});
const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      loginLoading.value = true;
      store
        .loginFn(ruleForm)
        .then(() => {
          loginLoading.value = false;
          emits("push", true);
        })
        .catch(() => {
          loginLoading.value = false;
        });
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>
<template>
  <div class="card">
    <div class="title">账号登录</div>
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      status-icon
      @keyup.enter="submitForm(ruleFormRef)"
    >
      <el-form-item label="账号" prop="mobile">
        <el-input v-model="ruleForm.mobile" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" />
      </el-form-item>
      <el-form-item>
        <el-button
          :loading="loginLoading"
          style="width: 100%; height: 40px; font-size: 18px"
          type="primary"
          @click="submitForm(ruleFormRef)"
        >
          登录
        </el-button>
      </el-form-item>
    </el-form>
    <el-divider />
    <div class="loginSelect">
      <el-button class="item" @click="emits('update:type', 'mobile')"
        >手机号登录</el-button
      >
    </div>
  </div>
</template>
